#include "lib/Time.h"
#include <sys/time.h>
#include <sstream>
#include "modules/AssistModule/LogModule.h"

namespace behavior_controller
{
    namespace Time
    {
        double Now()
        {
            timeval tv;
            if (gettimeofday(&tv, nullptr) == -1)
            {
                LogPub::Info("behavior_controller", "[Time] Now:-1");
            }
            return tv.tv_sec + (tv.tv_usec * 1e-6);
        }

        std::string Text()
        {
            std::string time_str;

            timeval tv;
            tm *time;

            if (gettimeofday(&tv, nullptr) == -1)
            {
                LogPub::Info("behavior_controller", "[Time] Text:-1");
            }
            time = localtime(&(tv.tv_sec));
            char systime[40];
            strftime(systime, 40, "%F_%T", time);

            std::ostringstream oss;
            oss << tv.tv_usec * 1e-6;
            std::string usec_str = oss.str();
            time_str = systime + usec_str.substr(1, 4);
            return time_str;
        }

    } // namespace Time
} // namespace behavior_controller